package com.yuntu.controller;

import com.alibaba.fastjson.JSONArray;
import com.mysql.jdbc.StringUtils;
import com.yuntu.dao.PageUtil;
import com.yuntu.pojo.Peitems;
import com.yuntu.service.PeitemsService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/peitems")
public class PeitemsController {
    private Logger logger = Logger.getLogger(PeitemsController.class);
    @Autowired
    private PeitemsService peitemsService;
    @RequestMapping(value = "/getPage",method = RequestMethod.GET)
    public String getPage(Integer pageIndex, HttpSession session)throws Exception{
        System.out.println("----------getPage");
        if (pageIndex==null){
            pageIndex = 1;
        }
        PageUtil<Peitems> pageUtil = new PageUtil<>();
        pageUtil.setPageindex(pageIndex);
        pageUtil.setPagesize(1);
        peitemsService.getPageAll(pageUtil);
        session.setAttribute("pageUtil",pageUtil);
        return "frame";
    }

    @RequestMapping("/insertsave")
    public String insertsave(){
        return "addpeitems";
    }
    @RequestMapping("/insert")
    public String insert(@Valid Peitems peitems)throws Exception{
        if (peitemsService.insert(peitems)){
            return "redirect:/peitems/getPage";
        }else{
            return "addpeitems";
        }
    }
    @RequestMapping(value = "/getAllById/{itemid}",method = RequestMethod.GET)
    public String getAllByIdUpd(@PathVariable int itemid, Model model)throws Exception{
        Peitems peitems = peitemsService.getAllById(itemid);
        model.addAttribute(peitems);
        return "updpeitems";
    }
    @RequestMapping(value = "/peitemsUpd",method = RequestMethod.POST)
    public String peitemsUpd(Peitems peitems)throws Exception{
        if (peitemsService.peitemsUpd(peitems)){
            return "redirect:/peitems/getPage";
        }
        return "updpeitems";
    }
    @RequestMapping(value = "deleteById/{itemid}",method = RequestMethod.GET)
    public String deleteById(@PathVariable int itemid)throws Exception{
        if (peitemsService.deleteById(itemid)){
            return "redirect:/peitems/getPage";
        }
        return "redirect:/peitems/getPage";
    }
    @ResponseBody
    @RequestMapping("/selectNotRepeat.do")
    public Object selectNotRepeat(@RequestParam("itemname") String itemname)throws Exception{
        Map<String,String> map = new HashMap<>();
        if (StringUtils.isNullOrEmpty(itemname)){
            map.put("itemname","exist");
        }else {
            Peitems peitems = peitemsService.selectNotRepeat(itemname);
            if (peitems!=null){
                map.put("itemname","exist");
            }else{
                map.put("itemname","noexist");
            }
        }
        return JSONArray.toJSONString(map);
    }


}
